home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-19 | 3.1 KB | 154 lines | [TEXT/CWIE] |
- /*
- Problem 01 - Mode Sort
-
- This problem is to sort an input string of N characters, N<1000000, based on
- the number of times a character occurs in the input. The most frequently
- occurring character should be sorted to the front of the string, followed by
- the next most frequently occurring character, etc. For characters occurring
- the same number of times, the character that occurs first in the input should
- be sorted to the front.
-
- Header specification
-
- pascal OSErr ModeSort( const FSSpec* infile, const FSSpec* outfile );
-
- Input specification
-
- The infile input file contains the characters. Input characters other than
- those printable low ascii characters c, 0x20<c<0x7f, must be ignored.
-
- Output specification
-
- The outfile must be created and then filled with the sorted characters. It's
- final length should be exactly the same as the count of characters in the
- allowed range (0x20<c<0x7f) (which may be shorter than the infile file length).
-
- Sample input
-
- abcdefghabbcccdddeee
- or
- 012345678911234567892123456789312345678941234567895123456789612345678971234567891
-
- Sample output
-
- ccccddddeeeebbbaafgh
- or
- 111111111122222222233333333344444444455555555566666666677777777788888888999999990
- */
-
- #include "Solution.h"
-
- // Fill in your solution and then submit this folder
-
- // Team Name: Jager & Quinn
-
- pascal OSErr ModeSort( const FSSpec* infile, const FSSpec* outfile )
- {
- long index = 0;
- Boolean char_order[128];
- long char_count[128];
- short refnum;
- long count;
- static char buffer[8192];
- for(long k = 0; k < 128; k++)
- {
- char_order[k] = 0;
- char_count[k] = 0;
- }
-
- OSErr err = FSpOpenDF(infile, fsRdPerm,&refnum);
- if (err == noErr)
- {
-
- do {
- count = sizeof(buffer);
- err = FSRead(refnum,&count,buffer);
- if ( (err != noErr) && (err != eofErr) )
- {
- break;
- }
- for(long i=0; i< count; i++)
- {
- char c = buffer[i];
-
- if ( (0x20 < c) && (c < 0x7f) )
- {
- if (char_count[c] == 0)
- {
- char_order[c] = index;
- index++;
- }
- char_count[c]++;
- }
- }
- } while (err == noErr);
-
- FSClose(refnum);
-
- if (err == eofErr)
- {
- err = noErr;
- }
-
- if (err == noErr)
- {
- FSpCreate(outfile,'CWIE','TEXT',0);
- err = FSpOpenDF(outfile, fsRdWrPerm,&refnum);
- }
-
- if (err == noErr)
- {
- Boolean done = false;
- SetEOF(refnum,0);
-
- while (!done)
- {
- char which = 0;
- for(long i = 0x21; i < 0x7f; i++)
- {
- if (char_count[i] != 0)
- {
- if ( (which == 0) || (char_count[i] > char_count[which]) )
- {
- which = i;
- } else if ( (which != 0) && (char_count[i] == char_count[which]) &&
- (char_order[i] < char_order[which]) )
- {
- which = i;
- }
- }
- }
- if (which != 0)
- {
- while (char_count[which] > 0)
- {
- count = char_count[which];
- if ( count > sizeof(buffer) )
- {
- count = sizeof(buffer);
- }
-
- for(long j = 0; j < count; j++) {
- buffer[j] = which;
- }
-
- err = FSWrite(refnum,&count,buffer);
- if (err != noErr)
- {
- break;
- }
- char_count[which] -= count;
- }
- }
- else
- {
- done = true;
- }
- }
- }
- FSClose(refnum);
- }
-
- return err;
- }
-